//Se realiza la importacion de las librerias de java que usaremos en nuestra aplicacion

package algoritmo;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.JComponent;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;


/********************************************************************************/
/***********************|************AUTORES************|************************/
/***********************|Marina Auxiliadora Toruño Pérez|************************/
/***********************|Melvin Rafael Vega Vega        |************************/
/********************************************************************************/

//Definicion de la clase principal
public class algoritmo extends JFrame{
	int x;
        JTabbedPane jtp;
        JTextArea jta;
        JLabel jl[]=new JLabel[50];
        JMenuItem jmi[]=new JMenuItem[50];
        String cadenas[]=new String[50];
        File file[]=new File[50];
        JPanel jp=new JPanel();
        JButton jb=new JButton("X");
	public algoritmo() {
        jtp=new JTabbedPane();
        jtp.setTabLayoutPolicy(jtp.SCROLL_TAB_LAYOUT);
        getContentPane().setLayout(null);
        setBounds(0, 0, 500, 500);
        x=-1;
        jta=new JTextArea();
        JPanel jp2=new JPanel();
        jb.setMargin(new Insets(0, 2, 0, 1));
        jp.add(Box.createHorizontalStrut(6));
        jp.add(jb);
        WindowListener l = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
        };
        //Parser x;
        addWindowListener(l);
        JMenuBar jmb=new JMenuBar();
        ImageIcon icon = new ImageIcon("agregar.gif");
        JMenu jm=new JMenu("Archivo");
        jmi[3]=new JMenuItem("Nuevo");
        jmi[3].setIcon(icon);
        jmi[0]=new JMenuItem("Abrir");
        jmi[1]=new JMenuItem("Guardar");
        jmi[2]=new JMenuItem("Guardar como");
        jmi[1].setEnabled(false);
        jmi[0].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e)
           {
            JFileChooser fileChooser = new JFileChooser();
            int seleccion = fileChooser.showOpenDialog(jta);
            File fichero=null;
            if (seleccion == JFileChooser.APPROVE_OPTION)
            {
             fichero = fileChooser.getSelectedFile();
            }
            BufferedReader input;
            String line="";
            String cadena="";
            try {
            //System.out.print(fichero.getPath());
            x++;
            jl[x]=new JLabel(fichero.getName());
            jtp.addTab(fichero.getName(),jp);
            jtp.setComponentAt(jtp.getSelectedIndex(), jb);
            jtp.setSelectedIndex(jtp.getTabCount()-1);
            jtp.addChangeListener(new ChangeListener() {
		public void stateChanged(ChangeEvent evento2) {
		jta.setText(cadenas[jtp.getSelectedIndex()]);
	    }
            }
            );
            input = new BufferedReader(new FileReader(fichero.getPath()));
            while ((line=input.readLine()) != null)
            {
                cadena =cadena+"\n "+line;
            }

            input.close();
            }
            catch(IOException err) {
            //whatever you want it to do when you get an error
            }
            cadenas[jtp.getSelectedIndex()]=new String(cadena);
            file[jtp.getSelectedIndex()]=fichero;
            jta.setText(cadena);
            jta.setEnabled(true);
            jmi[1].setEnabled(true);
           }
        });
         jmi[1].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt5)
           {
               try{
                 cadenas[jtp.getSelectedIndex()]=jta.getText();
                 Writer out = new BufferedWriter(new FileWriter(file[jtp.getSelectedIndex()]));
                 out.write(cadenas[jtp.getSelectedIndex()]);
                 out.close();

               }
               catch(IOException ex)
               {
                   
               }
           }
         });
         jmi[2].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt6)
           {
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setSelectedFile(new File("Nuevo Archivo"));
            int seleccion = fileChooser.showSaveDialog(jta);
            File fichero=null;
            if (seleccion == JFileChooser.APPROVE_OPTION)
            {
             fichero = fileChooser.getSelectedFile();
            }
            cadenas[jtp.getSelectedIndex()]=jta.getText();
               try{
                 file[jtp.getSelectedIndex()]=fichero;
                 jtp.setTitleAt(jtp.getSelectedIndex(), fichero.getName());
                 cadenas[jtp.getSelectedIndex()]=jta.getText();
                 Writer out = new BufferedWriter(new FileWriter(file[jtp.getSelectedIndex()]));
                 out.write(cadenas[jtp.getSelectedIndex()]);
                 out.close();

               }
               catch(IOException ex)
               {
                   
               }
           }
         });
         jmi[3].addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent evt7)
           {
            x++;
            jl[x]=new JLabel("New File "+jtp.getTabCount());
            jtp.add("New File "+jtp.getTabCount(),jl[x]);
            jtp.setSelectedIndex(jtp.getTabCount()-1);
            cadenas[jtp.getSelectedIndex()]="";
            jta.setEnabled(true);
           }
         });
        jm.add(jmi[3]);
        jm.add(jmi[0]);
        jm.add(jmi[1]);
        jm.add(jmi[2]);
        jmb.add(jm);
        jmb.setAlignmentX(10);
        jmb.setBounds(0, 0, 500, 20);
        jtp.setBounds(0,25 , 500, 20);
        jtp.setAlignmentX(JTabbedPane.LEFT_ALIGNMENT);
        jta.setBounds(20, 50, 460, 460);
        JScrollPane scroll = new JScrollPane(jta, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setBounds(20, 50, 460, 460);
        JScrollBar scrollBar = new JScrollBar();
        scroll.setCorner(JScrollPane.LOWER_RIGHT_CORNER, scrollBar);
        getContentPane().add(scroll);
        getContentPane().add(jmb);
        getContentPane().add(jtp);
        setVisible(true);
        jta.setEnabled(false);
	}
    public static void main(String args[]) {
            algoritmo frm=new algoritmo();
    }

}  